const app = getApp()
Page({
  onLoad() {
    this.setData({
      classify: app.classify
    })
    console.log(this.data.classify)
  },
  classifyChange(e) {
    let index = e.detail.value;
    this.setData({
      classifyIndex: index,
      classifyId: this.data.classify[index]._id
    })
    console.log(this.data.classifyId)
  },
  answerChange(e) {
    this.setData({
      answer: e.detail.value
    })
  },
  titleChange(e) {
    this.setData({
      title: e.detail.value
    })
  },
  data: {
    classify: [],
    classifyIndex: 0,
    classifyId: '0',
    title: "",
    answer: "",
    dialogShow: false,
    dialogMsg: "正在加载...",
    buttons: [{ text: "确定" }]
  },
  tapDialogButton() {
    this.setData({
      dialogShow: false
    })
  },
  back() {
    wx.navigateBack({
      delta: 0,
    })
  },
  submitQuestion() {
    this.setData({
      dialogShow: true,
      dialogMsg: "正在提交...",
      buttons: []
    })
    let {
      classifyId,
      answer,
      title
    } = this.data;

    if (!title || !answer) {
      this.setData({ dialogShow: true, dialogMsg: "请将内容填写完整", buttons: [{ text: '确定' }] })
      return
    }

    let db = wx.cloud.database();
    const _ = db.command
    const col = db.collection('interviewDetail');
    col.add({
      // data 字段表示需新增的 JSON 数据
      data: {
        // _id: 'todo-identifiant-aleatoire', // 可选自定义 _id，在此处场景下用数据库自动分配的就可以了
        classifyId,
        title,
        answer,
        userInfo: app.globalData.userInfo,
        due: new Date().toLocaleDateString() + new Date().toLocaleTimeString(),
      }
    }).then(res => {
      const col = db.collection('topList');
      let userName = app.globalData.userInfo.nickName;
      let avatarUrl = app.globalData.userInfo.avatarUrl
      col.where({
        userName
      })
        .get()
        .then(res => {
          console.log(res)
          if (res.data.length) {
            wx.cloud.callFunction({
              // 云函数名称
              name: 'update',
              // 传给云函数的参数
              data: {
                "collectionName": "topList",
                "key": "Num",
                "_id": res.data[0]._id
              }
            })
          } else {
            col.add({
              data: {
                userName,
                avatarUrl,
                Num: 1,
                createTime: new Date().toDateString() + " " + new Date().toLocaleTimeString()
              }
            })
          }
        })
      wx.cloud.callFunction({
        // 云函数名称
        name: 'update',
        // 传给云函数的参数
        data: {
          "collectionName": "interviewClassify",
          "_id": this.data.classifyId,
          "key": "count"
        }
      })
        .then(res => {
          console.log(res.result) // 3
          this.setData({
            dialogShow: false,
            title: "",
            answer: ""
          })
          wx.navigateTo({
            url: '/pages/handleSuccess/handleSuccess',
          })
        })
        .catch(err => {
          console.log(err)
          this.setData({
            dialogMsg: "提交失败,网络错误",
            buttons: [{ text: '确定' }]
          })
        })
    })
      .catch(err => {
        console.log(err)
        this.setData({
          dialogMsg: "提交失败,网络错误",
          buttons: [{ text: '确定' }]
        })
      })
  }
});